﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO.Compression;

public partial class Default4 : System.Web.UI.Page
{
    ClassExecute.ClassExecute dt;
    static string error;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //if ((Session["username"] != null) || (Session["username"].ToString()==""))
            //{
              MultiView1.ActiveViewIndex = 0;
                
            //}
            //else
            //{
            //    Response.Redirect("http://thanhnien.com.vn");
            //}
        }
    }
   
    protected void Button1_Click1(object sender, EventArgs e)
    {
        if (TextBox1.Text == "khongcanxin")
        {
            MultiView1.ActiveViewIndex = 1;
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            dt = new ClassExecute.ClassExecute();
            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_tinhds", CommandType.StoredProcedure,
                new SqlParameter("@cmd", TextBox2.Text));
            if (db.Rows.Count > 0)
            {
                GridView1.DataSource = db;
                GridView1.DataBind();
            }
        }
        catch 
        {
            return;
        }
    }
    protected void btBK_Click(object sender, EventArgs e)
    {
        if (File.Exists(Server.MapPath("~/FileInventory/DB.bak")))
        {
            File.Delete(Server.MapPath("~/FileInventory/DB.bak"));
        }

       dt=new ClassExecute.ClassExecute();
       bool kq=dt.ExecuteSQl_bool(str,CommandType.StoredProcedure,"k_backup",ref error,
           new SqlParameter("@path",Server.MapPath("~/FileInventory/DB.bak")));

       TextBox2.Text = error;
    }
    protected void btCompress_Click(object sender, EventArgs e)
    {
        if (File.Exists(Server.MapPath("~/FileInventory/DB.zip")))
        {
            File.Delete(Server.MapPath("~/FileInventory/DB.zip"));
        }

        FileStream sourceFile = File.OpenRead(Server.MapPath("~/FileInventory/DB.bak"));
        FileStream destFile = File.Create(Server.MapPath("~/FileInventory/DB.zip"));

        GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress);

        try
        {
            int theByte = sourceFile.ReadByte();
            while (theByte != -1)
            {
                compStream.WriteByte((byte)theByte);
                theByte = sourceFile.ReadByte();
            }
        }
        finally
        {
            compStream.Dispose();
        } 
    }
}
